<template>
  <div style="height: 100%; width: 100%">
  <div class="img">
    <div class="mask"></div>
  </div>
  <div class="login">
    <div v-loading="loading" element-loading-text="登录中..." element-loading-spinner="el-icon-loading"
         element-loading-background="rgba(0, 0, 0, 0)">
        <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
          <h3 class="login-title"><small style="font-size: 16px; ">欢迎登录</small><br>宠达CRM系统</h3>
          <el-form-item label="账号" prop="username">
            <el-input type="text" placeholder="请输入账号" v-model="form.userName"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" placeholder="请输入密码" v-model="form.password"/>
          </el-form-item>
          <el-form-item style="padding-left: 65px">
            <el-button type="primary" style="background: #003366; border: none" v-on:click="onSubmit('loginForm')">登录</el-button>
          </el-form-item>
        </el-form>

      <el-dialog
        title="温馨提示"
        :visible.sync="dialogVisible"
        width="30%">
        <span>请输入账号和密码</span>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
      </el-dialog>

      <el-dialog
        title="温馨提示"
        :visible.sync="dialogVisibleError"
        width="30%">
        <span style="color: red">请输入正确的账号和密码</span>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisibleError = false">确 定</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        form: {
          userName: 'heroc',
          password: ''
        },

        // 表单验证，需要在 el-form-item 元素中增加 prop 属性
        rules: {
          userName: [
            {required: true, message: '账号不可为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不可为空', trigger: 'blur'}
          ]
        },

        // 对话框显示和隐藏
        dialogVisible: false,
        dialogVisibleError: false,
        loading: false,

        info: {

        }
      }
    },
    methods: {
      onSubmit(formName) {
        var param = {
          'userid': this.form.userName,
          'pwd': this.form.password
        }
        // 为表单绑定验证功能
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loading = true,
            this.axios({
              url: '/api/login',
              method: "post",
              data: JSON.stringify({
                userName: param.userid,
                password: param.pwd,
                passID: sessionStorage.getItem('passID')
              }),
              headers: {
                'Content-Type': 'application/json; charset=utf-8'
              }
              // responseType: 'text'
            }).then((response)=>{
              if(response.data.statusCode == "200"){
                sessionStorage.setItem("passID",response.data.data.passID);
                localStorage.setItem("userName",param.userid);
                this.$router.push("/index/main");
              }else {
                this.loading = false;
                this.dialogVisibleError = true;
                return false;
              }
            })
          } else {
            this.dialogVisible = true;
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss" scoped>

  @-webkit-keyframes scrollToUp {
    0% {
      background-position: 0 0;
    }
    100% {
      background-position: 0 -300px;  // 向上滚动则就是图片的高度的负值，否则为图片的宽度，也可以百分比
    }
  }
  @keyframes scrollToUp {
    0% {
      background-position: 0 0;
    }
    100% {
      background-position: 0 -300px;
    }
  }

  .login {
    width: 100%;
    height: 100%;
    position: absolute;
  }

  .img{
    width: 100%;
    height: 2000px;
    background-image: url("../../static/images/loginbackground.png");
    background-size: 80% auto;
    background-attachment: fixed; /* 让背景图片和文本脱离*/
    background-position: center center;
    -webkit-animation: scrollToUp 25s linear infinite; /* 引入 scrollToUp动画  linear 表示匀速执行动画可避免卡顿*/
    animation: scrollToUp 25s linear infinite;
    overflow: hidden;
    position: fixed;
    z-index: -1;
  }

  .mask{
    width: 100%;
    min-height: 100%;
    background:rgba(0,0,0,.4);
    position: fixed;
  }

  .login-box {
    background-color: #ffffff;
    border: 1px solid #DCDFE6;
    width: 350px;
    margin: 170px auto;
    padding: 35px 35px 15px 35px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    box-shadow: 0 0 25px #909399;
  }

  .login-title {
    text-align: center;
    margin: 0 auto 40px auto;
    color: #303133;
  }
</style>

